//
//  DBJPersonFootView.m
//  DBJ
//
//  Created by 肖浩然的mac on 2016/11/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJPersonFootView.h"
#import <Masonry.h>
#import "DBJPersonModalModel.h"

@interface DBJPersonFootView ()<UIPickerViewDelegate,UIPickerViewDataSource>

//数据源
@property (strong,nonatomic)NSArray *array;
//城市数据源
@property (strong,nonatomic)NSArray *cities;
//当前的省城
@property(nonatomic, assign) NSInteger currentSelectedProvice;

//要得到的相应的省和城市
@property(nonatomic,copy)NSString *name;

@property(nonatomic,copy)NSString *city;

@end
@implementation DBJPersonFootView

-(void)dbjSetupSubviews{
     [self loadProvinceData];
    //创建UIView
    UIView *label = [[UIView alloc]init];
    label.backgroundColor = [UIColor zbh_colorWithRed:239 green:239 blue:243];
    [self addSubview:label];
    CGFloat W = self.bounds.size.width;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(W, 50));
        make.top.left.right.mas_equalTo(self);
        
    }];
    
    //创建UIBotton
    UIButton *btn = [[UIButton alloc]init];
    [btn setTintColor:[UIColor whiteColor]];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"确定" forState:UIControlStateNormal ];

    //添加点击事件
    [btn addTarget:self action:@selector(clickbtn) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [label addSubview:btn];
    [label insertSubview:btn atIndex:0];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(label).offset(-15);
        make.size.mas_equalTo(CGSizeMake(50, 50));
//        make.bottom.mas_equalTo(label);
        make.centerY.mas_equalTo(label);
    }];

    //    创建视图
    UIPickerView *pickView = [[UIPickerView alloc]init];
    pickView.backgroundColor = [UIColor whiteColor];
    //设置代理
    pickView.delegate = self;
    pickView.dataSource = self;
    pickView.showsSelectionIndicator = YES;
    [self  addSubview:pickView];
    
    //布局pickView
    [pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(self.bounds.size.width, 200));
        make.top.mas_equalTo(label.mas_bottom);
        make.left.right.mas_equalTo(self);
    }];
}

//有几列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    //(2列)
    return 2;
}

//有几行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    //component(列数)
    if (component == 0) {
        //返回省份个数
        return self.array.count;
    }else{
        //返回城市相应的个数
        DBJPersonModalModel *Model = self.array[self.currentSelectedProvice];
        return Model.cities.count;
    }
}

//设置每一列的内容   返回值是一个字符串
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        //展示省份
        DBJPersonModalModel  *Model = self.array[row];
        
        //记录
        self.name = Model.name;
        return Model.name;
    }else{
        //展示相应的城市
        DBJPersonModalModel  *Model = self.array[self.currentSelectedProvice];
        //记录
        self.city = Model.cities[row];
        return Model.cities[row];
    }
}
//点击选中的方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    //刷新当前省份的城市数据
    if(component == 0){
        self.currentSelectedProvice = row;
        [pickerView reloadComponent:1];
    }
}


//按钮的点击事件
-(void)clickbtn{
    //NSLog(@"%@--%@",self.name,self.city);
    if (self.labelBlock != nil) {
        NSString *data = [NSString stringWithFormat:@"%@--%@",self.name,self.city];
        self.labelBlock(data);
    }
}


#pragma mark - 加载数据
-(void)loadProvinceData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    self.cities = arr;
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSDictionary *dict in arr) {
        // 省
        DBJPersonModalModel *model = [DBJPersonModalModel provinceWithDict:dict];
        [arrM addObject:model];
    }
    _array = arrM;
}

@end
